//
//  LFLaunchAnimateView.m
//  AntFarms
//
//  Created by LF on 2017/3/7.
//  Copyright © 2017年 GG. All rights reserved.
//

#import "LFLaunchAnimateView.h"

#define kDuration 0.4

@interface LFLaunchAnimateView()

//**  <#description#> */
@property (nonatomic, assign) NSInteger index;

//**  <#description#> */
@property (nonatomic, strong) NSMutableArray *imgArr;

//**  <#description#> */
@property (nonatomic, strong) NSMutableArray *topViewArr;

//**  <#description#> */
@property (nonatomic, strong) NSTimer *timer;

@end

@implementation LFLaunchAnimateView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (NSTimer *)timer
{
    if (!_timer) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:kDuration
                                                  target:self
                                                selector:@selector(addtopView)
                                                userInfo:nil
                                                 repeats:YES];
    }
    return _timer;
}

- (void) setupUI
{
    self.index = 0;
    self.imgArr = [[NSMutableArray alloc]init];
    self.topViewArr = [[NSMutableArray alloc]init];
    
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ic_launch"]];
    imageView.backgroundColor = [UIColor backGroundGrayColor];
    imageView.frame = self.bounds;
    
    CGFloat tW = kNumFrom667(196);
    CGFloat tH = kNumFrom667(21);
    CGFloat tX = (kScreenW - tW) * 0.5;
    CGFloat tY = kNumFrom667(307);
    
    UIView *textView = [[UIView alloc]init];
    textView.frame = CGRectMake(tX, tY, tW, tH);
    
    NSArray *imgArray = @[
                          @"ic_laucher_a",
                          @"ic_laucher_n",
                          @"ic_laucher_t",
                          @"ic_laucher_f",
                          @"ic_laucher_a",
                          @"ic_laucher_r",
                          @"ic_laucher_m"
                          ];
    
    CGFloat imgX;
    CGFloat imgY = kNumFrom667(2);
    CGFloat imgW = tH - kNumFrom667(2);
    CGFloat imgH = imgW;
    CGFloat margin = kNumFrom667(8);
    CGFloat extra = 0;
    
    for (int i = 0; i < 7; i++) {
        imgX = i *(imgW + margin);
        extra = i > 2 ? kNumFrom667(15) : 0;
        imgX += extra;
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(imgX, imgY, imgW, imgH)];
        imageView.image = [UIImage imageNamed:imgArray[i]];
        [textView addSubview: imageView];
        
        UIView *slideView = [[UIView alloc]initWithFrame:imageView.frame];
        slideView.backgroundColor = [UIColor backGroundGrayColor];
        slideView.alpha = 0;
        [textView addSubview:slideView];
        
        [self.imgArr addObject:imageView];
        [self.topViewArr addObject:slideView];
    }
    
    UILabel *textLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, kScreenH-60, kScreenW, 50)];
    textLabel.textAlignment = NSTextAlignmentCenter;
    textLabel.font = [UIFont systemFontOfSize:kNumFrom667(13)];
    textLabel.textColor = [UIColor lf_colorWithHexString:@"#C3C3C3"];
    textLabel.text = @"Copyright©2017 沐熙集团";
    
    [self addSubview:imageView];
    [self addSubview:textView];
    [self addSubview:textLabel];
    
    [self.timer setFireDate:[NSDate distantPast]];
}

- (void) addtopView
{
    if (self.index == self.topViewArr.count) {
        [self.timer setFireDate:[NSDate distantFuture]];
        [self.timer invalidate];
         self.timer = nil;
        
        [UIView animateWithDuration:0.5 animations:^{
            self.transform = CGAffineTransformMakeScale(1.3, 1.3);
            self.alpha = 0;
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
        }];
        
        return;
    }
    
    UIView *aView = self.topViewArr[self.index];
    [UIView animateWithDuration:kDuration animations:^{
        aView.alpha = 0.9;
    } completion:^(BOOL finished) {
        aView.alpha = 0;
    }];
    
    self.index++;
}

@end
